/**
 * @file     longestWord.js
 * @brief    [720. 词典中最长的单词](https://leetcode-cn.com/problems/longest-word-in-dictionary/)
 * @author   Zhu
 * @date     2022-03-17 23:14
 */

// TODO: Trie

/**
 * @param {string[]} words
 * @return {string}
 */
var longestWord = function (words) {
    const dict = [['']];
    let i = 1;
    while (true) {
        const iWords = words
            .filter((s) => s.length == i)
            .filter((s) => dict[i - 1].some((d) => s.includes(d)))
            .sort();
        if (iWords.length == 0) break;
        dict[i] = iWords;
        i++;
    }

    return dict[i - 1][0];
};
